"use strict";

/*
**
* 页面初始化时加载 window.onload
*/
var page = 1,
    defulSize = 12,
    catalIndex = 1,
    allPage = void 0;
function loadLesson(index, reslut) {
    if (index == 1 || index == 2 || index == 3) {
        //专家课程、保险讲堂
        var obj = reslut.data;
        var html = ''; //最多显示多少条
        for (var i = 0; i < obj.length; i++) {
            var priceHtml = "";
            if (obj[i].priceStr == 0) {
                priceHtml = "<span>免费</span>";
            } else {
                priceHtml = "<span>\uFFE5" + obj[i].priceStr + "</span>";
            }
            html += "<div class=\"classFox\" data-id=\"" + obj[i].courseId + "\" data-index=\"" + index + "\">\n                  <div class='ani_imgBox'>  <img src=\"" + obj[i].poster + "\" alt=\"\">\n       </div>             <div class=\"className\">" + obj[i].title + "</div>\n                    <div class=\"classnum\">\u5171" + obj[i].catalogCount + "\u8282 </div>\n                    <div class=\"classpirce\">" + priceHtml + " " + obj[i].buyCount + "\u4EBA\u8D2D\u4E70</div>\n                </div>";
        }
        $("#classBox").html(html);
        $("#classBox .classFox").click(function (e) {
            var id = e.currentTarget.dataset.id;
            var index = e.currentTarget.dataset.index;
            location.href = "trainDetail.html?id=" + id + "&index=" + index;
        });
    } else if (index == 4) {
        //直播课程
        var obj1 = reslut.data;
        var html1 = ''; //最多显示多少条
        for (var _i = 0; _i < obj1.length; _i++) {
            html1 += "<div class=\"lessonItem flexLay\" >\n                        <div class=\"lessonLeft\">\n                          <div class='ani_imgBox'>  <img src=\"" + obj1[_i].poster + "\" alt=\"\">\n        </div>                    <div class=\"lessonInfo\" data-id=\"" + obj1[_i].courseId + "\" data-index=\"" + index + "\">\n                                <div class=\"lessonTit\" >" + obj1[_i].title + "</div>\n                                <div class=\"lessonTecher\">\n                                    <img src=\"" + obj1[_i].lecturerAvatar + "\" alt=\"\">\n                                    <span>" + obj1[_i].lecturerName + "</span>\n                                </div>\n                            </div>\n                        </div>\n                        <div class=\"lessonRight comflex\">\n                            <div class=\"timer\">" + obj1[_i].liveTime + "</div>\n                            <div class=\"appointment\" id=\"appointment\" onclick=\"appointment(" + obj1[_i].courseId + ")\">\u9884\u7EA6</div>\n                        </div>\n                    </div>";
        }

        $("#lessonBox").html(html1);
        $(".lessonInfo").click(function (e) {
            var id = e.currentTarget.dataset.id;
            var index = e.currentTarget.dataset.index;
            location.href = "trainDetail.html?id=" + id + "&index=" + index;
        });
    }
    allPage = reslut.totalPage;
    getPageNav(reslut.totalPage);
}

//上一页
function prevPage() {
    if (page == 1) {
        return toastr.info("没有上一页了喔~");
    }
    page--;
    serverLoad(catalIndex, defulSize);
}
//下一页
function nextPage() {
    if (page >= allPage) {
        return toastr.info("没有下一页了喔~");
    }
    page++;
    serverLoad(catalIndex, defulSize);
}

//首页
function firstPage() {
    if (page == 1) {
        return toastr.info("已经在首页了喔~");
    }
    page = 1;
    serverLoad(catalIndex, defulSize);
}

//尾页
function lastPage() {
    if (page == allPage) {
        return toastr.info("已经在尾页了喔~");
    }
    page = allPage;
    serverLoad(catalIndex, defulSize);
}

// 点击GO进入任一页面
function goPage() {
    var val = $("#pageVal").val();
    if (!val || val <= 0 || val > allPage) {
        return toastr.info("不存在的页面~");
    }
    if (val == page) {
        return;
    }
    page = val;
    serverLoad(catalIndex, defulSize);
}

//加载新闻
function serverLoad(index, pageSize) {
    pageSize = pageSize || defulSize;
    $.server({ courseCategoryId: index, pageNum: page, pageSize: pageSize }, '/website/v1/queryCourseList').then(function (res) {
        loadLesson(index, res.result); //加载课程
    });
}
// 预约直播课程
function appointment(id) {
    $.server({ courseId: id }, '/iagent/v1/bookCourse').then(function (res) {
        console.log(res, "用户预约");
        location.href = "trainDetail.html?id=" + id;
        return toastr.info('预约成功');
    });
}

// 计算分页 点击培训分类时  页面加载时都要调用
// 计算分页 点击培训分类时  页面加载时都要调用
function getPageNav(totalPage) {
    var html = '';
    // const len = Math.ceil(Number(length) / Number(page));
    if(totalPage>10 && page>3){
      html+='...'
    }
    if(totalPage>10){
      for (var i = 0; i < totalPage; i++) {
        if( i >= page-3 && i< page+2){
          if(i+1 == page){
            html += "<div class='pageBox active' onclick= reloadNews(" + (i + 1) + ") > " + (i + 1) + " </div>";
          }else{
            html += "<div class=\"pageBox\" onclick= reloadNews(" + (i + 1) + ") > " + (i + 1) + " </div>";
          }
          
        } 
      }
      if(page<totalPage-3){
        html+='...'
      }
     
      if(page<totalPage-3){
        html += "<div class='pageBox' onclick= reloadNews(" + (totalPage) + ") > " + (totalPage) + " </div>";
      }
      $("#pageSizeBox").html(html);
    }else{
      for (var i = 0; i < totalPage; i++) {
        html += "<div class=\"pageBox\" onclick= reloadNews(" + (i + 1) + ") > " + (i + 1) + " </div>";
      }
      $("#pageSizeBox").html(html);
      $("#pageSizeBox>div").eq(page - 1).addClass("active");
    }
    
    
   
  }

//点击分页重新加载
function reloadNews(i) {
    page = i;
    serverLoad(catalIndex, defulSize);
}

// 页面顶部大图数据(待定)
function posterImg() {
    $.server({ location: 2 }, '/website/v1/getPoster').then(function (res) {
        console.log(res, "页面顶部大图数据");
        $('#trainHeader').attr("src", res.result.data[0].url);
    });
}

$(function () {
    toastr.options = {
        closeButton: false,
        debug: false,
        progressBar: true,
        positionClass: "toast-top-center",
        onclick: null,
        showDuration: "300",
        hideDuration: "1000",
        timeOut: "2000",
        extendedTimeOut: "1000",
        showEasing: "swing",
        hideEasing: "linear",
        showMethod: "fadeIn",
        hideMethod: "fadeOut"
    };
    //调用server加载首页新闻
    serverLoad(1, 12);
    posterImg();
    $("#classUl>li").click(function (e) {
        var index = e.currentTarget.dataset.index;
        if ($(this).hasClass('active')) {
            return;
        }
        catalIndex = index;
        $("#classUl li").removeAttr("class", "active");
        $(this).addClass("active");
        $("#babelTitle").text($(this).text());
        if (index == 1) {
            //
            defulSize = 12;
            serverLoad(index, defulSize);
            $("#classBox").fadeIn();
            $("#lessonBox").fadeOut();
            $("#trainHeader").attr({
                "src": "images/trainHeader.png",
                "class": 'animated pulse'
            });
        } else if (index == 2) {
            //保险讲堂
            $("#classBox").fadeIn();
            $("#lessonBox").fadeOut();
            defulSize = 12;
            serverLoad(index, defulSize);
        } else if (index == 3) {
            //
            $("#classBox").fadeIn();
            $("#lessonBox").fadeOut();
            defulSize = 12;
            serverLoad(index, defulSize);
        } else if (index == 4) {
            // 直播课堂
            defulSize = 10;
            serverLoad(index, defulSize);
            $("#classBox").fadeOut();
            $("#lessonBox").fadeIn();
            $("#trainHeader").attr({
                "src": "images/zhibo.png",
                "class": 'animated bounce'
            });
        }
    });
});